package org.gitos.cloud;

import org.gitos.cloud.domain.device.Router;
import org.gitos.cloud.domain.redis.DeviceRedisTemplate;
import org.gitos.cloud.domain.redis.ObjectRedisTemplate;
import org.gitos.cloud.util.ZkUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ImportResource;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;

/**
 * Created by shizhengchu on 16/3/1.
 */
@SpringBootApplication
@ImportResource("classpath:xml/dubbox.xml")
public class Application implements CommandLineRunner {
	private static final Logger log = LoggerFactory.getLogger(Application.class);

	@Autowired
	ApplicationContext ctx;

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}

	public void run(String... args) throws Exception {
		ZkUtil.init(ctx.getEnvironment().getProperty("zk.hosts", ""), ctx.getEnvironment().getProperty("zk.path", "/node"));
	}

	@Bean
	public ObjectRedisTemplate objectRedisTemplate(JedisConnectionFactory fc) {
		return new ObjectRedisTemplate(fc);
	}

	@Bean
	public DeviceRedisTemplate deviceRedisTemplate(JedisConnectionFactory fc) {
		return new DeviceRedisTemplate(fc);
	}

	@Bean
	public Router router(DeviceRedisTemplate redisTemplate) {
		Router router = new Router(redisTemplate);
		return router;
	}

}
